package com.cn.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

public class DateConverter  extends DefaultTypeConverter {
	private static final  DateFormat[] ACCEPT_DATE_FORMATS = {
		 new SimpleDateFormat("dd/MM/yyyy"),   
	        new SimpleDateFormat("yyyy-MM-dd"), 
	};

	public Object convertValue(Map context, Object value, Class toType) {
		
		if(toType==Date.class){
			Date date = null;
			String dateString = null;
			String[] params = (String[])value;
			dateString = params[0];
			for(DateFormat formate : ACCEPT_DATE_FORMATS){
				try {
					return formate.parse(dateString);
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					continue;
				}
		}
			return null;
			
			
		}
		else if(toType ==String.class){
			Date date =(Date)value;
			return new SimpleDateFormat("yyyy-MM-dd").format(date);
			
			
		}
		return null;
	}

	public String convertToString(Map arg0, Object arg1) {
		// TODO Auto-generated method stub
		return null;
	}

}
